package co.fitstart.mobile.web.controller.course;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.wechat.web.interceptor.WechatJsConfig;

import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.user.Student;
import co.fitstart.entity.user.StudentTargetData;
import co.fitstart.entity.user.User;
import co.fitstart.mobile.service.CourseRoundService;
import co.fitstart.mobile.service.StudentService;
import co.fitstart.mobile.service.UserService;
import co.fitstart.mobile.web.interceptor.MobileDetect;
import co.fitstart.mobile.web.interceptor.Router;
import co.fitstart.mobile.web.interceptor.WechatAuthority;
import co.fitstart.mobile.web.resolver.Secure;

/**
 *
 * @author Ark
 *
 */
@Controller
@RequestMapping("/profile")
public class StudentTargetController {
    
    private static final Logger logger = LoggerFactory.getLogger(StudentTargetController.class);
    
    @Autowired
    private CourseRoundService courseRoundService;
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private StudentService studentService;
    
    /**
     * @param user
     * @param courseId
     * @return
     */
    @RequestMapping(value = "/courses/{courseId}/targets", method = RequestMethod.GET)
    public ModelAndView list(@Secure User user,
                             @PathVariable("courseId") Long courseId) {
        
        ModelAndView modelAndView = new ModelAndView("course/student-target/student-target-list");
        
        Student student = userService.getStudent(user);
        CourseRound courseRound = courseRoundService.loadCourseRound(courseId);
        
        List<StudentTargetData> targets = studentService.listStudentTargetData(student, courseRound);
        modelAndView.addObject("targets", targets);
        
        return modelAndView;
        
    }

    /**
     * @param courseId
     * @param redirect
     * @return
     */
    @RequestMapping(value = "/courses/{courseId}/targets/create", method = RequestMethod.GET)
    @Router
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public ModelAndView create(@Secure User user,
                               @PathVariable("courseId") Long courseId,
                               @RequestParam("redirect") String redirect) {
        
        ModelAndView modelAndView = new ModelAndView("course/student-target/student-target-create");
        
        Student student = userService.getStudent(user);
        CourseRound courseRound = courseRoundService.loadCourseRound(courseId);
        
        StudentTargetData target = studentService.loadLatestStudentTargetData(student, courseRound);
        
        modelAndView.addObject("courseId", courseId);
        modelAndView.addObject("redirect", redirect);
        modelAndView.addObject("target", target);
        
        return modelAndView;
        
    }
    
    /**
     * @param user
     * @param courseId
     * @param target
     * @return
     */
    @RequestMapping(value = "/courses/{courseId}/targets", method = RequestMethod.POST)
    @Router
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public @ResponseBody String onCreate(@Secure User user,
                                         @PathVariable("courseId") Long courseId,
                                         StudentTargetData target) {
        
        Result result = new Result();
        
        try {
            
            // 1. load student and courseRound
            Student student = userService.getStudent(user);
            CourseRound courseRound = courseRoundService.loadCourseRound(courseId);
            
            // 2. save data to database
            studentService.create(target.initialize(student, courseRound));
            
            result.success().message("学员目标数据录入成功");
            
        } catch (Exception e) {
            logger.error("#onCreate() - create student target fail, exception is [{}]", e);
            result.fail("500").message("学员目标数据录入失败");
        }
        
        return JsonUtils.toJsonString(result);
        
    }
    
    @RequestMapping(value = "/courses/{courseId}/confirm-target", method = RequestMethod.POST)
    @Router
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public @ResponseBody String onConfirm(@Secure User user,
                                         @PathVariable("courseId") Long courseId,
                                         @RequestParam("confirm") boolean confirm) {
        
        Result result = new Result();
        
        try {
            CourseRound courseRound = courseRoundService.loadCourseRound(courseId);
            courseRoundService.fullInTarget(courseRound, confirm);
            
            result.success().message("确认是否填入目标数据成功");
            
        } catch (Exception e) {
            logger.error("#onConfirm - ", e);
            result.fail("500").message("确认是否填入目标数据失败");
        }
        
        return JsonUtils.toJsonString(result);
        
    }
    
}
